﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace EVTEST
{
    class Led_xc5k1
    {
        public static Mutex mutex = new Mutex();
        public System.IO.Ports.SerialPort ComPort_2;
        private byte esc = 0x1b;
        private byte clr = 0x44;
        private byte over = 0x0d;
        private byte pinhao = 1;
        public byte Pinhao
        {
            get
            {
                return pinhao;
            }

            set
            {
                pinhao = value;
            }
        }
        #region 初始化串口
        /// <summary>
        /// 初始化串口
        /// </summary>
        /// <param name="PortName">串口名字</param>
        /// <param name="LinkString">连接字符串 如9600,N,8,1</param>
        /// <returns>bool</returns>
        public bool Init_Comm(string PortName, string LinkString, byte tjph)
        {
            try
            {
                Pinhao = tjph;
                ComPort_2 = new System.IO.Ports.SerialPort();
                if (ComPort_2.IsOpen)
                    ComPort_2.Close();
                ComPort_2.PortName = PortName;
                ComPort_2.BaudRate = int.Parse(LinkString.Split(',').GetValue(0).ToString());
                switch (LinkString.Split(',').GetValue(1).ToString().ToUpper())
                {
                    case "N":
                        ComPort_2.Parity = System.IO.Ports.Parity.None;
                        break;
                    case "O":
                        ComPort_2.Parity = System.IO.Ports.Parity.Odd;
                        break;
                    case "E":
                        ComPort_2.Parity = System.IO.Ports.Parity.Even;
                        break;
                    case "M":
                        ComPort_2.Parity = System.IO.Ports.Parity.Mark;
                        break;
                    case "S":
                        ComPort_2.Parity = System.IO.Ports.Parity.Space;
                        break;
                    default:
                        ComPort_2.Parity = System.IO.Ports.Parity.None;
                        break;
                }
                ComPort_2.DataBits = int.Parse(LinkString.Split(',').GetValue(2).ToString());
                switch (LinkString.Split(',').GetValue(3).ToString())
                {
                    case "1":
                        ComPort_2.StopBits = System.IO.Ports.StopBits.One;
                        break;
                    case "2":
                        ComPort_2.StopBits = System.IO.Ports.StopBits.Two;
                        break;
                    default:
                        ComPort_2.StopBits = System.IO.Ports.StopBits.One;
                        break;
                }
                ComPort_2.Open();
                if (ComPort_2.IsOpen)
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                ComPort_2 = null;
                return false;
            }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ledString"></param>
        /// <param name="position">1-32 点阵上排 2-32 点阵下排 3-16 点阵第一排 4-16 点阵第二排 5-16 点阵第三排 6-16 点阵第四排</param>
        /// <param name="type">1-静止显示 2-向左滚筒 3-向右滚筒 4-向上滚动 5-向下滚动</param>
        public void xcledShowText(string ledString, byte position, byte type)
        {
            try
            {
                mutex.WaitOne();
                try
                {
                    byte[] ledStringArray = System.Text.Encoding.GetEncoding("GB2312").GetBytes(ledString.Trim());
                    byte[] content = new byte[6 + ledStringArray.Length];
                    content[0] = esc;
                    content[1] = Pinhao;
                    content[2] = 0x0b;
                    content[3] = position;
                    content[4] = type;
                    for (byte i = 5; i < 5 + ledStringArray.Length; i++)
                    {
                        content[i] = ledStringArray[i - 5];
                    }
                    content[5 + ledStringArray.Length] = over;
                    ComPort_2.Write(content, 0, 6 + ledStringArray.Length);
                }
                catch
                { }
                mutex.ReleaseMutex();
            }
            catch
            { }
        }
        public void xcledClear()
        {
            try
            {
                mutex.WaitOne();
                try
                {
                    byte[] content = new byte[4];
                    content[0] = esc;
                    content[1] = Pinhao;
                    content[2] = 0x44;
                    content[3] = over;
                    ComPort_2.Write(content, 0, 4);
                }
                catch
                { }
                mutex.ReleaseMutex();
            }
            catch
            { }
        }

        public void xcledRecoverStartup()
        {
            try
            {
                mutex.WaitOne();
                try
                {
                    byte[] content = new byte[4];
                    content[0] = esc;
                    content[1] = Pinhao;
                    content[2] = 0x0f;
                    content[3] = over;
                    ComPort_2.Write(content, 0, 4);
                }
                catch
                { }
                mutex.ReleaseMutex();
            }
            catch
            { }
        }
        Bitmap bmp_xlz = new Bitmap(256, 64);
        bool isinit_xlz = false;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="datas">为 32*64bytes 数据，从左到右，从上到下，代表 256*64 点阵数据</param>
        /// <param name="type">1-静止显示 2-向左滚筒 3-向右滚筒 4-向上滚动 5-向下滚动</param>
        public void xcledShowJPG(Bitmap jpg, byte type)
        {
            try
            {
                mutex.WaitOne();
                try
                {

                    byte[] leddata = new byte[32 * 64];
                    //int k = 0;
                    for (int i = 0; i < 64; i++)
                    {
                        for (int j = 0; j < 32; j++)
                        {
                            leddata[i * 32 + j] = 0x00;
                            for (int k = 0; k < 8; k++)
                            {
                                if (j * 8 + k >= jpg.Width || i >= jpg.Height)
                                    break;
                                Color pixelcolor = bmp_xlz.GetPixel(j * 8 + k, i);
                                if (pixelcolor.R + pixelcolor.G + pixelcolor.B >= 255)
                                {
                                    leddata[i * 24 + j] += (byte)(1 << (7 - k));
                                }
                            }
                        }
                    }
                    byte[] content = new byte[6 + leddata.Length];
                    content[0] = esc;
                    content[1] = Pinhao;
                    content[2] = 0x0b;
                    content[3] = 0;
                    content[4] = type;
                    for (byte i = 5; i < 5 + leddata.Length; i++)
                    {
                        content[i] = leddata[i - 5];
                    }
                    content[5 + leddata.Length] = over;
                    ComPort_2.Write(content, 0, 6 + leddata.Length);
                }
                catch
                { }
                mutex.ReleaseMutex();
            }
            catch
            { }
        }
        public void closeLed()
        {
            try
            {
                if (ComPort_2.IsOpen)
                    ComPort_2.Close();
            }
            catch
            { }
        }
    }
}
